package ch.cm.adnroid.cleverlearn.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseManager extends SQLiteOpenHelper {

    public static final String DB_NAME = "questions.db";
    public static final int DB_VERSION = 1;
    public static final String QUESTION_TABLE_COLUMN_ID = "_id";
    public static final String QUESTION_TABLE_COLUMN_YES_NO = "Y_N";
    public static final String QUESTION_TABLE_COLUMN_QUESTIONTEXT = "Q_TEXT";
    public static final String QUESTION_TABLE_COLUMN_ANSWERTEXT = "A_TEXT";
    public static final String QUESTION_TABLE_COLUMN_RIGHTCOUNT = "COUNT";
    public static final int QUESTION_TABLE_COLUMN_COUNT = 5;
    
    public static final String INFO_TABLE_NAME = "sqlite_sequence";
    public static final String INFO_TABLE_NAME_COLUMN = "name";

    public static final String DROP_TABLE = "DROP TABLE IF EXISTS ";

    public DatabaseManager(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void createTable(SQLiteDatabase db, String tableName) {
        String createTable = "CREATE TABLE " + tableName + "(" + QUESTION_TABLE_COLUMN_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + QUESTION_TABLE_COLUMN_QUESTIONTEXT + " TEXT NOT NULL,"
                + QUESTION_TABLE_COLUMN_ANSWERTEXT + " TEXT NOT NULL," + QUESTION_TABLE_COLUMN_YES_NO
                + " BOOLEAN NOT NULL," + QUESTION_TABLE_COLUMN_RIGHTCOUNT + " INTEGER NOT NULL)";
        db.execSQL(createTable);
    }

    public void dropTable(SQLiteDatabase database, String tableName) {
        database.execSQL(DROP_TABLE + tableName);
    }

    public static String[] getAllTableColumns() {
        String[] arr = new String[QUESTION_TABLE_COLUMN_COUNT];
        int i = 0;
        arr[i++] = QUESTION_TABLE_COLUMN_ID;
        arr[i++] = QUESTION_TABLE_COLUMN_QUESTIONTEXT;
        arr[i++] = QUESTION_TABLE_COLUMN_ANSWERTEXT;
        arr[i++] = QUESTION_TABLE_COLUMN_YES_NO;
        arr[i++] = QUESTION_TABLE_COLUMN_RIGHTCOUNT;
        return arr;
    }
}
